package com.yikome.utils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HtmlUtil;
import cn.hutool.http.HttpRequest;

public class RomaniaZipCode {

	
//	public static void main(String[] args) {
//		
//		ArrayList<String> Judets = Lists.newArrayList("Alba", "Arad", "Argeș", "Bacău", "Bihor", "Bistrița-Năsăud", "Botoșani", "Brăila", "Brașov", "Bucuresti", "Buzău", "Călărași", "Caraș-Severin", "Cluj", "Constanța", "Covasna", "Dambovița", "Dolj", "Galați", "Giurgiu", "Gorj", "Harghita", "Hunedoara", "Ialomița", "Iași", "Ilfov", "Maramureș", "Mehedinți", "Mureș", "Neamț", "Olt", "Prahova", "Sălaj", "Satu Mare", "Sibiu", "Suceava", "Teleorman", "Timiș", "Tulcea", "Valcea", "Vaslui", "Vrancea");
//	
//		List<Map<String, String>> cityDatas = new ArrayList<>();
//		
//		for(int i = 0; i < Judets.size(); i++) {
//			
//			
//			System.out.println(Judets.get(i) + "start=========================>" + LocalDateTime.now());
//			
//			Integer judetId = (21 + i) * 10000;
//			
//			ImmutableMap<String, String> judetData = ImmutableMap.of("id", String.valueOf(judetId), "name", Judets.get(i), "parentId", String.valueOf(100000));
//			
//			cityDatas.add(judetData);
//			
//			String localitateResponse = HttpRequest.post("https://www.posta-romana.ro/cnpr-app/modules/cauta-cod-postal/ajax/cauta_orase.php")
//					.form("k_judet", Judets.get(i))
//					.form("k_lang", "ro")
//					.execute()
//					.body();
//			
//			Map<String, String> localitateResponseParse = (Map<String, String>) JSONUtils.parseJson(localitateResponse);
//			String formular = localitateResponseParse.get("formular");
//			
//			formular = HtmlUtil.removeHtmlAttr(formular, "class", "value");
//			
//			formular = StrUtil.replace(formular, "<option>", "");
//			
//			formular = StrUtil.replace(formular, "</option>", ",");
//			
//			formular = formular.substring(0, formular.length() - 1);
//			
//			List<String> localitates = StrUtil.split(formular, ',');
//			localitates.remove(0);
//			
//			
//			for (int j = 0; j < localitates.size(); j++) {
//				
//				System.out.println(Judets.get(i) + "--->" + localitates.get(j));
//				
//				String postCodeResponse = HttpRequest.post("https://www.posta-romana.ro/cnpr-app/modules/cauta-cod-postal/ajax/cautare_pentru_cod.php")
//						.form("k_judet", Judets.get(i))
//						.form("k_localitate", localitates.get(j))
//						.form("k_lang", "ro")
//						.execute()
//						.body();
//				
//				
//				String postCode = StrUtil.subWithLength(postCodeResponse, 147, 6);
//				
//				
//				ImmutableMap<String, String> localitateData = ImmutableMap.of("id", String.valueOf(judetId + j + 1), "name", localitates.get(j), "parentId", String.valueOf(judetId), "postCode", postCode);
//				
//				System.out.println(localitateData);
//				
//				cityDatas.add(localitateData);
//			}
//			
//			System.out.println(Judets.get(i) + "end=========================>" + LocalDateTime.now());
//		}
//		System.out.println(JSONUtils.toJsonString(cityDatas));
//		
//		FileUtil.appendString(JSONUtils.toJsonString(cityDatas), "D:/ro/1111.txt", CharsetUtil.CHARSET_UTF_8);
//	}
	
	
	
	
//	public static void main(String[] args) {
//		
//		ArrayList<String> Judets = Lists.newArrayList("Alba", "Arad", "Argeș", "Bacău", "Bihor", "Bistrița-Năsăud", "Botoșani", "Brăila", "Brașov", "Bucuresti", "Buzău", "Călărași", "Caraș-Severin", "Cluj", "Constanța", "Covasna", "Dambovița", "Dolj", "Galați", "Giurgiu", "Gorj", "Harghita", "Hunedoara", "Ialomița", "Iași", "Ilfov", "Maramureș", "Mehedinți", "Mureș", "Neamț", "Olt", "Prahova", "Sălaj", "Satu Mare", "Sibiu", "Suceava", "Teleorman", "Timiș", "Tulcea", "Valcea", "Vaslui", "Vrancea");
//	
//		List<Map<String, String>> cityDatas = new ArrayList<>();
//		
//		for(int i = 0; i < Judets.size(); i++) {
//			
//			
//			System.out.println(Judets.get(i) + "start=========================>" + LocalDateTime.now());
//			
//			Integer judetId = (21 + i) * 10000;
//			
//			ImmutableMap<String, String> judetData = ImmutableMap.of("id", String.valueOf(judetId), "name", Judets.get(i), "parentId", String.valueOf(100000));
//			
//			cityDatas.add(judetData);
//			
//			String localitateResponse = HttpRequest.post("https://www.posta-romana.ro/cnpr-app/modules/cauta-cod-postal/ajax/cauta_orase.php")
//					.form("k_judet", Judets.get(i))
//					.form("k_lang", "ro")
//					.execute()
//					.body();
//			
//			Map<String, String> localitateResponseParse = (Map<String, String>) JSONUtils.parseJson(localitateResponse);
//			String formular = localitateResponseParse.get("formular");
//			
//			formular = HtmlUtil.removeHtmlAttr(formular, "class", "value");
//			
//			formular = StrUtil.replace(formular, "<option>", "");
//			
//			formular = StrUtil.replace(formular, "</option>", ",");
//			
//			formular = formular.substring(0, formular.length() - 1);
//			
//			List<String> localitates = StrUtil.split(formular, ',');
//			localitates.remove(0);
//			
//			
//			for (int j = 0; j < localitates.size(); j++) {
//				
////				String postCodeResponse = HttpRequest.post("https://www.posta-romana.ro/cnpr-app/modules/cauta-cod-postal/ajax/cautare_pentru_cod.php")
////						.form("k_judet", Judets.get(i))
////						.form("k_localitate", localitates.get(j))
////						.form("k_lang", "ro")
////						.execute()
////						.body();
////				
////				
////				String postCode = StrUtil.subWithLength(postCodeResponse, 147, 6);
//				
//				
//				ImmutableMap<String, String> localitateData = ImmutableMap.of("id", String.valueOf(judetId + j + 1), "name", localitates.get(j), "parentId", String.valueOf(judetId), "parentName", Judets.get(i));
//				
//				cityDatas.add(localitateData);
//			}
//			
//			System.out.println(Judets.get(i) + "end=========================>" + LocalDateTime.now());
//		}
//		
//		FileUtil.appendString(JSONUtils.toJsonString(cityDatas), "D:/ro/1111.txt", CharsetUtil.CHARSET_UTF_8);
//	}
	
	
	public static void main(String[] args) {
		String cityDataFile = FileUtil.readUtf8String("D:/ro/1111.txt");
		List<Map> parseJsonList = JSONUtils.parseJsonList(cityDataFile, Map.class);
		
		for (Map map : parseJsonList) {
			
			String parentId = Convert.toStr(map.get("parentId"));
			
			if(!StrUtil.equals("100000", parentId)) {
				
				String postCode = null;
				
				while (true) {
					
					if(postCode != null) break;
					
					try {
						String postCodeResponse = HttpRequest.post("https://www.posta-romana.ro/cnpr-app/modules/cauta-cod-postal/ajax/cautare_pentru_cod.php")
								.form("k_judet", Convert.toStr(map.get("parentName")))
								.form("k_localitate", Convert.toStr(map.get("name")))
								.form("k_lang", "ro")
								.execute()
								.body();
						postCode = StrUtil.subWithLength(postCodeResponse, 147, 6);
						map.put("postCode", postCode);
						
					}catch (Exception e) {
						System.out.println(e.getMessage());
					}
				}
			}
		}
		
		FileUtil.appendString(JSONUtils.toJsonString(parseJsonList), "D:/ro/2222.txt", CharsetUtil.CHARSET_UTF_8);
	}
}
